//号码位信息描述
  	  var phone_num_desc = {};
  	  
  	  var ADDBASE = 'add_base';
  	  
  	  var SUBBASE = 'sub_base';
  	  
  	  var SAMEBASE = 'same_base';
  	  
  	  var PHONENUMINDEX = 'index';
  	  
  	  var PHONENUMTEXT = 'text';
  	  
  	  var PHONENUMTYPE = 'type';
  	  
  	  var SAMETARGET = 'same_target';
  	  
  	  var ADDTARGET = 'add_target';
  	  
  	  var SUBTARGET = 'sub_target';
  	  
  	  //保存号码描述信息
  	var setPhoneNumText = function(index,text){
  		if(phone_num_desc[index]){
  			phone_num_desc[index][PHONENUMTEXT] = text;
  		}else{
  			phone_num_desc[index] = {'text':text};
  		}	
  	}
  	
  	var setPhoneNumType = function(index,text){
  		if(phone_num_desc[index]){
  			phone_num_desc[index][PHONENUMTYPE] = text;
  		}else{
  			phone_num_desc[index] = {'type':text};
  		}	
  	}
  	var setPhoneNumAddbase = function(index,text){
  		if(phone_num_desc[index]){
  			phone_num_desc[index][ADDBASE] = text;
  		}else{
  			phone_num_desc[index] = {'add_base':text};
  		}	
  	}
  	var setPhoneNumSubbase = function(index,text){
  		if(phone_num_desc[index]){
  			phone_num_desc[index][SUBBASE] = text;
  		}else{
  			phone_num_desc[index] = {'sub_base':text};
  		}	
  	}
  	var setPhoneNumSamebase = function(index,text){
  		if(phone_num_desc[index]){
  			phone_num_desc[index][SAMEBASE] = text;
  		}else{
  			phone_num_desc[index] = {'same_base':text};
  		}	
  	}
  	var setPhoneNumSameTarget = function(index,text){
  		if(phone_num_desc[index]){
  			phone_num_desc[index][SAMETARGET] = text;
  		}else{
  			phone_num_desc[index] = {'same_target':text};
  		}	
  	}
  	var setPhoneNumAddTarget = function(index,text){
  		if(phone_num_desc[index]){
  			phone_num_desc[index][ADDTARGET] = text;
  		}else{
  			phone_num_desc[index] = {'add_target':text};
  		}	
  	}
  	var setPhoneNumSubTarget = function(index,text){
  		if(phone_num_desc[index]){
  			phone_num_desc[index][SUBTARGET] = text;
  		}else{
  			phone_num_desc[index] = {'sub_target':text};
  		}	
  	}
  	
  	//选择与第n位相同时，如果目标位为*，获取当前列表中最大的字母code值，并+2，返回对应的字母
  	var getCharOfSamen = function(){
  	  		
  	  		var maxCharCode = 'A'.charCodeAt(0);
  	  		
  	  		for(var index in phone_num_desc){
  	  			if(phone_num_desc[index][PHONENUMTEXT].charCodeAt(0) > maxCharCode){
  	  				maxCharCode = phone_num_desc[index][PHONENUMTEXT].charCodeAt(0);
  	  			}
  	  		}
  	  	
  	  		return String.fromCharCode(maxCharCode == 'A'.charCodeAt(0) ? maxCharCode : (maxCharCode+2));
  	  }
  
  	  $('#num_length').focus().keypress(function(event){
  	  		
  	  		if(event.which == 13){
  	  			event.preventDefault();
  	  			
  	  			var length = $(this).val();
  	  			
  	  			if($.isNumeric(length)){
	  	  			$('#num_detail > td').each(function(){
	  	  				$(this).unbind('click');
	  	  				$(this).remove();
	  	  				for(var index in phone_num_desc){
	  	  					delete phone_num_desc[index];
	  	  				}
	  	  			});
	  	  			$('#num_detail_head > td').each(function(){
	  	  				
	  	  				$(this).remove();
	  	  				
	  	  			});
	  	  			$(this).removeClass('errorbox');
  	  				for(var i=0;i<length;i++){
  	  					$('#num_detail').append('<td id="phoneNum'+i+'" class="btn" style="height:28px;width:28px;font-size:20px;text-align:center;" vAlign="middle"></td>');
  	  					
  	  					$('#num_detail_head').append('<td style="text-align:center;" vAlign="middle">'+(i+1)+'</td>');
  	  					
  	  					$('#num_detail > td').click(function(e){
  	  						e.preventDefault();
  	  						$('#num_detail > td').each(function(){
  	  							$(this).removeClass('btnalt').addClass('btn');
  	  						});
  	  						$(this).removeClass('btn').addClass('btnalt');
  	  					});
  	  				}
  	  				
  	  				$('#num_control').slideDown(300,function(){
  	  					$('#num_detail > td:first').click();
  	  				});
  	  			}else{
  	  				$(this).addClass('errorbox');
  	  			}
  	  			
  	  			
  	  		}
  	  });
  	  
  	  
  	
  	//重置号码类型下拉列表
  	var resetPhoneNumType = function(){
  		$('#num_type > option').removeAttr('selected');
  	  	$('#num_type > option').first().attr('selected','selected');
  	}
  	
  	//递减时递归更改前一个号码
  	var modifyPre = function(currentIndex){
  	
  						if(phone_num_desc[currentIndex-1] &&
  							( phone_num_desc[currentIndex-1][PHONENUMTYPE] == 'sub' || phone_num_desc[currentIndex-1][SUBBASE])){
  							
  							var text = String.fromCharCode($('#phoneNum'+currentIndex).text().charCodeAt(0)+1);
  	  						
  	  						$('#phoneNum'+(currentIndex-1)).text(text);
  	  						
  	  						setPhoneNumText(currentIndex-1,text);
  	  						
  	  						if(phone_num_desc[currentIndex-1][PHONENUMTYPE] == 'sub'){
  	  							setPhoneNumSubTarget(currentIndex,phone_num_desc[currentIndex-1][SUBTARGET]);
  	  							modifyPre(currentIndex-1);
  	  						}else{
  	  							setPhoneNumSubbase(currentIndex-1,true);
  	  							setPhoneNumSubTarget(currentIndex,currentIndex-1);
  	  						}
  							
  						}
  	  				}  
  	  
	$('#num_type').change(function(){
			var total_num = parseInt($('#num_length').val());
			
  	  		var selected_phone_num = $('#num_detail').find('td.btnalt').first();
  	  		
  	  		var index = parseInt(selected_phone_num.attr('id').substring(8));
  	  		
  	  		var type = $('#num_type option:selected').attr('value');
  	  		
  	  		var phoneNumIdBase = '#phoneNum';
  	  		
  	  		
  	  		if(type == ''){
  	  			return;
  	  		}
  	  		
  	  		if(!$.isNumeric(total_num)){
  	  			resetPhoneNumType();
  	  			return;
  	  		}
  	  		
  	  		if(type != 'samen'){
  	  			$(this).find('input').first().val('');		
  	  			$('#num_target').hide(300);	
  	  		}
  	  		
  	  		if(type == 'same'){
  	  			if(index > 0 && phone_num_desc[index-1]){
  	  			
  	  				if(phone_num_desc[index-1][PHONENUMTEXT] == '*'){
  	  				
  	  					var text = getCharOfSamen();
  	  					
  	  					$(phoneNumIdBase+(index-1)).text(text);
  	  					setPhoneNumText(index-1,text);
  	  					
  	  				}
  	  			
  	  				selected_phone_num.text(phone_num_desc[index-1][PHONENUMTEXT]);
  	  				
  	  				setPhoneNumText(index,phone_num_desc[index-1][PHONENUMTEXT]);
  	  			
  	  				setPhoneNumType(index,'same');
  	  				
  	  				setPhoneNumSameTarget(index,index-1);
  	  				
  	  				setPhoneNumSamebase(index-1,true);
  	  			}else{
  	  				resetPhoneNumType();
  	  				return;
  	  			}
  	  			
  	  		}else if(type == 'add'){
  	  			if(index > 0 && phone_num_desc[index-1]){
  	  			
  	  				if($.isNumeric(phone_num_desc[index-1][PHONENUMTEXT])){
  	  					var old_value = parseInt(phone_num_desc[index-1][PHONENUMTEXT]);
  	  					if(old_value == 9){
  	  						old_value = -1;
  	  					}
  	  					selected_phone_num.text(old_value+1);
  	  					setPhoneNumText(index,selected_phone_num.text());
  	  					setPhoneNumType(index,selected_phone_num.text());
  	  				}else{
  	  			
  	  					if(phone_num_desc[index-1][PHONENUMTEXT] == '*'){
  	  				
  	  						var text = 'A';
  	  					
  	  						$(phoneNumIdBase+(index-1)).text(text);
	  	  					setPhoneNumText(index-1,text);
  		  					
  	  						setPhoneNumAddTarget(index,index-1);
  	  					}
  	  				
  	  					selected_phone_num.text(String.fromCharCode(phone_num_desc[index-1][PHONENUMTEXT].charCodeAt(0)+1));
  	  				
	  	  				setPhoneNumText(index,selected_phone_num.text());
  	  			
  		  				setPhoneNumType(index,'add');
  	  				
  	  					if(phone_num_desc[index-1][PHONENUMTYPE] != 'add'){
  	  						setPhoneNumAddbase(index-1,true);
  	  						setPhoneNumAddTarget(index,index-1);
  	  					}
  	  				
	  	  				if(!phone_num_desc[index][ADDTARGET] && phone_num_desc[index][ADDTARGET] != 0){
  		  					setPhoneNumAddTarget(index,phone_num_desc[index-1][ADDTARGET]);
  	  					}
  	  				}
  	  			}else{
  	  				resetPhoneNumType();
  	  				return;
  	  			}
  	  			
  	  		}else if(type == 'sub'){
  	  			if(index > 0 && phone_num_desc[index-1]){
  	  				
  	  				if($.isNumeric(phone_num_desc[index-1][PHONENUMTEXT]) && parseInt(phone_num_desc[index-1][PHONENUMTEXT]) > 0){
  	  					selected_phone_num.text(parseInt(phone_num_desc[index-1][PHONENUMTEXT])-1);
  	  					setPhoneNumText(index,selected_phone_num.text());
  	  					setPhoneNumType(index,selected_phone_num.text());
  	  				}
  	  				else if(phone_num_desc[index-1][PHONENUMTEXT] == '*'){
  	  					selected_phone_num.text('A');
  	  					setPhoneNumText(index,'A');
  	  					setPhoneNumType(index,'sub');
  	  					setPhoneNumSubTarget(index,index-1);
  	  					
  	  					$(phoneNumIdBase+(index-1)).text('B');
  	  					setPhoneNumText(index-1,'B');
  	  					setPhoneNumSubbase(index-1,true);
  	  				}
  	  			
  	  				else if(phone_num_desc[index-1][PHONENUMTEXT].charCodeAt(0) > 'A'.charCodeAt(0)){
  	  					selected_phone_num.text(String.fromCharCode(phone_num_desc[index-1][PHONENUMTEXT].charCodeAt(0)-1));
  	  					setPhoneNumText(index,selected_phone_num.text());
  	  					setPhoneNumType(index,'sub');
  	  					if(phone_num_desc[index-1][PHONENUMTYPE] != 'sub'){
  	  						setPhoneNumSubbase(index-1,true);	
  	  						setPhoneNumSubTarget(index,index-1);
  	  					}else{
  	  						setPhoneNumSubTarget(index,phone_num_desc[index-1][SUBTARGET]);
  	  					}
  	  					
  	  				}
  	  				
  	  				else if(phone_num_desc[index-1][PHONENUMTEXT] == 'A' && phone_num_desc[index-1][SUBTARGET] >= 0 && phone_num_desc[index-1][PHONENUMTYPE] == 'sub' && phone_num_desc[phone_num_desc[index-1][SUBTARGET]][PHONENUMTYPE] == '*'){
  	  					for(var i=phone_num_desc[index-1][SUBTARGET];i<index;i++){
  	  						var text_now = $(phoneNumIdBase+i).text();
  	  						$(phoneNumIdBase+i).text(String.fromCharCode(text_now.charCodeAt(0)+1));
  	  						setPhoneNumText(i,$(phoneNumIdBase+i).text());
  	  					}
  	  					
  	  					selected_phone_num.text('A');
  	  					setPhoneNumText(index,'A');
  	  					setPhoneNumType(index,'sub');
  	  					setPhoneNumSubTarget(index,phone_num_desc[index-1][SUBTARGET]);
  	  				}else{
  	  					resetPhoneNumType();
  	  					return;
  	  				}
  	  						
  	  			}else{
  	  				resetPhoneNumType();
  	  				return;
  	  			}
  	  			
  	  			
  	  		}else if(type == 'samen'){
  	  			$('#num_target').show(300);	
  	  			$('#num_target').find('input').first().focus();
  	  		}else{
  	  			selected_phone_num.text(type);
  	  			setPhoneNumText(index,type);
  	  			setPhoneNumType(index,type);
  	  		}
  	  		
  	  		
  	  		
  	  		resetPhoneNumType();
  	  		
  	  		if(index < (total_num-1) && type != 'samen'){
  	  			$(phoneNumIdBase+(index+1)).click();
  	  		}
  	  		
  	  		
  	  		
  	  		$(selected_phone_num).nextAll('#num_detail > td').text('');
  	  		
  	  		for(var i in phone_num_desc){
  	  			if(i > index){
  	  				delete phone_num_desc[i];
  	  			}
  	  		}
  	  });
  	  
  	  

	  $('#num_target').keypress(function(event){
  	  		
  	  		if(event.which == 13){
  	  			event.preventDefault();
  	  			
  	  			var total_num = parseInt($('#num_length').val());
  	  			var target = parseInt($(this).find('input').first().val());
  	  			
  	  			if($.isNumeric(target) && target > 0 && target <= total_num && phone_num_desc[target-1]){
  	  				var selected_phone_num = $('#num_detail').find('td.btnalt').first();
  	  				var index = parseInt(selected_phone_num.attr('id').substring(8));
  	  				
  	  				if($.isNumeric(phone_num_desc[target-1][PHONENUMTEXT])){
  	  					selected_phone_num.text(phone_num_desc[target-1][PHONENUMTEXT]);
  	  					setPhoneNumText(index,phone_num_desc[target-1][PHONENUMTEXT]);
  	  					setPhoneNumType(index,phone_num_desc[target-1][PHONENUMTEXT]);
  	  				}else{
  	  					var text = phone_num_desc[target-1][PHONENUMTEXT];
  	  					
  	  					if(text == '*'){
  	  						text = getCharOfSamen();
  	  						setPhoneNumText(target-1,text);
  	  						$('#phoneNum'+(target-1)).text(text);
  	  					}
  	  					
  	  					
  	  					selected_phone_num.text(text);
  	  					setPhoneNumText(index,text);
  	  					setPhoneNumType(index,'same');
  	  					setPhoneNumSameTarget(index,target-1);
  	  				}
  	  				
  	  				if(selected_phone_num.next('#num_detail > td')){
  	  					selected_phone_num.next('#num_detail > td').click();
  	  				}
  	  			}else{
  	  				$('#num_target').find('input').first().addClass('errorbox');
  	  				$('#num_target').find('input').first().val('');
  	  				return;
  	  			}
  	  			$('#num_target').find('input').first().removeClass('errorbox');
  	  			$('#num_target').find('input').first().val('');		
  	  			$('#num_target').hide(300);	
  	  		}
  	  	});
  	  	
  	  var jsonObjToString = function(o){
  	  		var str = '{';
  	  
  	  		for(var tmp in o){
  	  			str += '"'+tmp+'":';
  	  			if($.isPlainObject(o[tmp])){
  	  				str += jsonObjToString(o[tmp]);
  	  			}else{
  	  				str += '"'+o[tmp]+'"';
  	  			}
  	  			
  	  			str += ',';
  	  		}
  	  		
  	  		return str.substring(0,str.length-1) + '}';
  	  		
  	  }
  	  
  	  $('#button_save').click(function(){
  		  	var price = $('#price').val();
  		  	
  		  	if(!$.isNumeric(price)){
				$('#price').addClass('errorbox');
				$('#price').nextAll('.smltxt').text('价格输入不正确');
				return;
			}else{
				$('#price').removeClass('errorbox');
				$('#price').nextAll('.smltxt').text('');
			}
  		  
  	  		var jsonMeta = '{"details":[';
  	  		
  	  		for(var index in phone_num_desc){
  	  			phone_num_desc[index]['index'] = index;
  	  			
  	  			jsonMeta += jsonObjToString(phone_num_desc[index]) + ',';
  	  		}
  	  		
  	  		jsonMeta = jsonMeta.substring(0, jsonMeta.length-1)+']}';
  	  		
  	  		$('#jsonMeta').val(jsonMeta);
  	  		
  	  		var phone_num_style = '';
  	  		
  	  		$('#num_detail > td').each(function(){
  	  			phone_num_style += $(this).text();
  	  		});
  	  		
  	  		$('#rule').val(phone_num_style);
  	  		
  	  		$('form').submit();
  	  });